package consumer;

import com.gitee.kamismile.stone.rpc.common.provider.consumer.service.IStoneService;
import com.gitee.kamismile.stone.rpc.grpc.common.GrpcCommonProto.ResultDTO;
import com.google.protobuf.Any;
import com.hans.grpcserver.grpc.HelloProto;
import com.hans.grpcserver.grpc.StoneGreeterGrpc;
import io.grpc.stub.StreamObserver;
import org.springframework.stereotype.Component;

@Component
public class GreeterImpl extends StoneGreeterGrpc.GreeterImplBase implements IStoneService {



    @Override
    public void sayHello(HelloProto.HelloRequest req, StreamObserver<ResultDTO> responseObserver) {
        HelloProto.HelloReply reply = HelloProto.HelloReply.newBuilder().setMessage("Hello " + req.getName()).build();
        System.out.println("aaaaa");
        // 使用响应监视器的onNext方法返回HelloReply

        ResultDTO build = ResultDTO.newBuilder().setData(Any.pack(reply)).build();
        responseObserver.onNext(build);
//        responseObserver.onError(Status.INTERNAL.withDescription("error desc").asRuntimeException());
//        try {
//            Thread.sleep(30000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        throw new BusinessException(1,"a");
        // 使用onCompleted方法指定本次调用已经完成
        responseObserver.onCompleted();
    }

    @Override
    public void sayBye(HelloProto.HelloRequest req, StreamObserver<ResultDTO> responseObserver) {
        HelloProto.HelloReply reply = HelloProto.HelloReply.newBuilder().setMessage("bye " + req.getName()).build();
        ResultDTO build = ResultDTO.newBuilder().setData(Any.pack(reply)).build();
        responseObserver.onNext(build);
//        responseObserver.onError(Status.INTERNAL.withDescription("error desc").asRuntimeException());
//        try {
//            Thread.sleep(30000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        throw new BusinessException(1,"a");
        // 使用onCompleted方法指定本次调用已经完成
        responseObserver.onCompleted();
    }
}
